package com.innovation.wjrpc.example;


import com.innovation.wjrpc.communication.HTTPTransportClient;
import com.innovation.wjrpc.communication.NettyTransportClient;
import com.innovation.wjrpc.rpc.HTTPRemoteInvoker;
import com.innovation.wjrpc.rpc.NettyRemoteInvoker;
import com.innovation.wjrpc.rpc.RpcClient;
import com.innovation.wjrpc.rpc.RpcClientConfig;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author innoyiya
 * @date 2021/3/6 16:46
 */
public class Client {
    static AtomicInteger atomicInteger = new AtomicInteger(0);
    public static void main(String[] args) throws InterruptedException {

        long start = System.currentTimeMillis();
//        netty(1);
        nettyAsync(1);
//
        Thread thread1 = new Thread(() -> {
            nettyAsync(1000);
        });
//        Thread thread2 = new Thread(() -> {
//            nettyAsync(10000);
//        });
//        Thread thread3 = new Thread(() -> {
//            nettyAsync(10000);
//        });
//        Thread thread4 = new Thread(() -> {
//            nettyAsync(10000);
//        });
//        Thread thread5 = new Thread(() -> {
//            nettyAsync(10000);
//        });
//        Thread thread6 = new Thread(() -> {
//            nettyAsync(10000);
//        });
//        Thread thread7 = new Thread(() -> {
//            nettyAsync(10000);
//        });
//        Thread thread8 = new Thread(() -> {
//            nettyAsync(10000);
//        });
//        Thread thread9 = new Thread(() -> {
//            nettyAsync(10000);
//        });
//        Thread thread10 = new Thread(() -> {
//            nettyAsync(10000);
//        });
//        thread1.start();
//        thread2.start();
//        thread3.start();
//        thread4.start();
//        thread5.start();
//        thread6.start();
//        thread7.start();
//        thread8.start();
//        thread9.start();
//        thread10.start();

//        thread1.join();
//        thread2.join();
//        thread3.join();
//        thread4.join();
//        thread5.join();
//        thread6.join();
//        thread7.join();
//        thread8.join();
//        thread9.join();
//        thread10.join();
        System.out.println(System.currentTimeMillis() - start + " ms");
        System.out.println(atomicInteger.get());
//        http();
    }

    public static void netty(int times){
        RpcClientConfig rpcClientConfig = new RpcClientConfig();
        rpcClientConfig.setTransportClass(NettyTransportClient.class);
        RpcClient client = new RpcClient(rpcClientConfig);
        CalcService service = client.getProxy(CalcService.class, NettyRemoteInvoker.class);
        int r1 = 0;
        int r2 = 0;
        for (int i = 0; i < times; i++) {
            r1 = service.add(1, 2);
            r2 = service.minus(2, 1);
        }
        //System.out.println(r1);
        //System.out.println(r2);
        System.out.println(r1 + r2);
    }

    public static void nettyAsync(int times){
        RpcClientConfig rpcClientConfig = new RpcClientConfig();
        rpcClientConfig.setTransportClass(NettyTransportClient.class);
        RpcClient client = new RpcClient(rpcClientConfig);
        CalcService service = client.getProxy(CalcService.class, NettyRemoteInvoker.class);
        int r1 = 0;
        int r2 = 0;
        for (int i = 0; i < times; i++) {

            CompletableFuture<Integer> addFuture = service.add(1, 2,true);
            CompletableFuture<Integer> minusFuture = service.minus(2, 1, true);

            try {
                r1 = addFuture.get();
                r2 = minusFuture.get();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
        System.out.println(r1 + r2);
    }


    public static void nettyAsyncOnMultithreading(int times, CountDownLatch countDownLatch) {
        Thread thread = new Thread(() -> {
            nettyAsync(times);
            countDownLatch.countDown();
        });
        thread.start();
    }

    public static void http(int times){
        RpcClientConfig rpcClientConfig = new RpcClientConfig();
        rpcClientConfig.setTransportClass(HTTPTransportClient.class);
        RpcClient client = new RpcClient(rpcClientConfig);
        CalcService service = client.getProxy(CalcService.class, HTTPRemoteInvoker.class);
        int r1 = 0;
        int r2 = 0;
        for (int i = 0; i < times; i ++) {
            r1 = service.add(1, 2);
            r2 = service.minus(2, 1);
        }

        System.out.println(r1);
        System.out.println(r2);
    }
}
